package edu.paper.job.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import edu.paper.job.dao.EmployeeDao;
import edu.paper.job.domain.Employee;
import edu.paper.job.domain.Job;
import edu.paper.job.domain.Resume;
import edu.paper.job.service.EmployeeService;
import edu.paper.job.service.JobService;
import edu.paper.job.service.ResumeService;

/**
 * 雇佣关系业务逻辑实现类
 * @author bowen
 *
 */
@Service
public class EmployeeServiceImpl implements EmployeeService{

	@Autowired
	private EmployeeDao employeeDao;
	
	@Autowired
	private JobService jobService;
	
	@Autowired
	private ResumeService resumeService;
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Override
	public List<Employee> find(Employee employee) {
		return employeeDao.find(employee);
	}

	@Override
	public Employee get(Serializable id) {
		return employeeDao.get(id);
	}

	@Override
	public void insert(Employee employee) {
		employeeDao.insert(employee);
	}

	@Override
	public void update(Employee employee) {
		employeeDao.update(employee);
	}

	@Override
	public void deleteById(Serializable id) {
		employeeDao.deleteById(id);
	}

	@Override
	public void delete(Serializable[] ids) {
		employeeDao.deleteById(ids);
	}

	@Override
	public List<Employee> listApplicantEmployee(Employee employee) {
		employee.setIsApplicantDeleted(false);
		return employeeDao.find(employee);
	}

	@Override
	public List<Employee> listCompanyEmployee(Employee employee) {
		employee.setIsCompanyDeleted(false);
		return employeeDao.find(employee);
	}

	@Override
	public void insertEmployee(Employee employee) {
		employee.setIsApplicantDeleted(false);
		employee.setIsCompanyDeleted(false);
		employee.setResult(0);
		employeeDao.insert(employee);
	}

	@Override
	public void deleteEmployeeByApplicant(Employee employee) {
		
		employee.setIsApplicantDeleted(true);
		employeeDao.update(employee);
		//判断是否求职者和企业都删除了该雇佣关系，如果都删除了就真删除该雇佣关系
		employee = employeeDao.get(employee.getId());
		if(employee.getIsCompanyDeleted() && employee.getIsApplicantDeleted()) {
			employeeDao.deleteById(employee.getId());
			deleteResumeOrJob(employee);
		}
		
	}

	

	private void deleteResumeOrJob(Employee employee) {
		/*
		 * 真删除招聘信息需要满足一些条件，企业删除了招聘信息，求职者不再浏览应聘信息。
		 */
		//检查招聘信息是否可以删除
		Job job = employee.getJob();
		if(job.getIsDeleted()) {//企业删除了该招聘信息
			//检查应聘者是否不再浏览招聘信息，即包含该招聘的雇佣关系都被应聘者删除了
			jobService.deleteJob(job);
		}
		
		/*
		 * 删除简历时要满足一些条件，求职者删除了简历，企业不再浏览应聘的简历。
		 */
		Resume resume = employee.getResume();
		if(resume.getIsDeleted()) {//求职者删除了简历
			
			//检查企业是否不再浏览招聘信息，即包含该简历的雇佣关系都被企业删除了
			resumeService.deleteResume(resume);
		}
		
		
	}

	@Override
	public void deleteEmployeeByCompany(Employee employee) {
		
		employee.setIsCompanyDeleted(true);
		employeeDao.update(employee);
		//判断是否求职者和企业都删除了该雇佣关系，如果都删除了就真删除该雇佣关系
		employee = employeeDao.get(employee.getId());
		if(employee.getIsCompanyDeleted() && employee.getIsApplicantDeleted()) {
			employeeDao.deleteById(employee.getId());
			deleteResumeOrJob(employee);
		}
	}

	@Override
	public void refuseApplicant(Employee employee) {
		employee.setResult(-1);
		employeeDao.update(employee);
	}

	@Override
	public void agreeApplicant(Employee employee) {
		employee.setResult(1);
		employeeDao.update(employee);
	}
	
	
}
